package com.java.springcloud.mybatisplus.conditions.segments;

import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.segments.AbstractISegmentList;
import com.java.springcloud.mybatisplus.enums.TdSqlKeyword;

import java.util.List;

import static java.util.stream.Collectors.joining;


/**
 * Fill SQL 片段
 *
 * @author zhangtao
 * @date 2022/04/02
 */
public class FillSegmentList extends AbstractISegmentList {

    @Override
    protected boolean transformList(List<ISqlSegment> list, ISqlSegment firstSegment, ISqlSegment lastSegment) {
        list.remove(0);
        if (!isEmpty()) {
            list.clear();
        }
        return true;
    }

    @Override
    protected String childrenSqlSegment() {
        if (isEmpty()) {
            return EMPTY;
        }
        return this.stream().map(ISqlSegment::getSqlSegment)
                .collect(joining(COMMA, SPACE + TdSqlKeyword.FILL.getSqlSegment() + LEFT_BRACKET, RIGHT_BRACKET));
    }
}
